package com.clss.commandhelper.tools.janus.util;


import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.clss.commandhelper.tools.Lg;

import org.webrtc.RendererCommon;
import org.webrtc.VideoRenderer.Callbacks;
import org.webrtc.VideoRendererGui;

import java.util.Stack;

public class VideoRendererUtil {
	int index;
	Stack<VideoRendererBean> videoRendererlList=new Stack<VideoRendererBean>();//局部坐标
	public int videoRendererWidth=100;
	public  int videoRendererHeight=100;
	public int screenWidth ;
	public int screenHeight;
	int videoRemoteRendererNumber=0;

	int columnNumber=2;//列数
	int rowNumber=3;//行数
	/**
	 * 初始化坐标数据
	 * @param screenWidth 屏幕宽度
	 * @param screenHeight	屏幕高度
	 */
	public VideoRendererUtil(Context context,FrameLayout fview,int screenWidth ,int screenHeight){
		this.context=context;
		this.fview=fview;
		this.screenWidth=screenWidth;
		this.screenHeight=screenHeight;
		for (int i = 0; i < rowNumber; i++) {//行数
			for (int j = 0; j < columnNumber; j++) {//列数
//    			System.out.println("j "+j+"   i "+i);
    			/*
    			 * 切割局部坐标
    			 * 	[ 0 ,0 ,50 ,33 ]		1行1列
					[ 50 ,0 ,50 ,33 ]		1行2列
					[ 0 ,33 ,50 ,33 ]		2行1列
					[ 50 ,33 ,50 ,33 ]		2行2列
					[ 0 ,66 ,50 ,33 ]		3行1列
					[ 50 ,66 ,50 ,33 ]		3行2列
    			 */
				VideoRendererBean videoRendererBean=new VideoRendererBean();
				if (j==0) {//1行
					videoRendererBean.setX(0);//1、2、3行1列X值均为0;
				}else{
					videoRendererBean.setX(videoRendererWidth/columnNumber);//2列的X值均为50(屏幕宽度除2)
				}
				videoRendererBean.setY(i*(videoRendererHeight/rowNumber));//从0开始以+30递增
				videoRendererBean.setWidth(videoRendererWidth/columnNumber);//1、2列的宽度均为50(屏幕宽度除2)
				if(i==2){//高度100/3最后余1，加到最后一行
					videoRendererBean.setHeight(videoRendererHeight/rowNumber+1);//1、2列的高度均为33(屏幕高度除3)
				}else{
					videoRendererBean.setHeight(videoRendererHeight/rowNumber);//1、2列的高度均为33(屏幕高度除3)
				}
				videoRendererlList.add(videoRendererBean);
//            	Log.e("VideoRendererUtil"+i, "videoRendererLocBean "+videoRendererLocBean.toString());
			}
		}
	}
	/**
	 *  设置远程渲染
	 * @param renderer  渲染器
	 * @param mirror 是否反转成像   false 不反转   true反转
	 */
	public void setRemoteRendererLoc(Callbacks renderer,boolean mirror,String data){
		for (int i = 0; i < videoRendererlList.size(); i++) {
			VideoRendererBean videoRendererBean=videoRendererlList.get(i);
			if(videoRendererBean.getRenderer()!=null&&videoRendererBean.getRenderer()==renderer){//unpublish的处理，如果已经存在则使用
				VideoRendererGui.update(renderer, videoRendererBean.getX(), videoRendererBean.getY(), videoRendererBean.getWidth(), videoRendererBean.getHeight(), RendererCommon.ScalingType.SCALE_ASPECT_FILL, mirror);
				return ;
			}
			if(videoRendererBean.getRenderer()==null){ //如果没占用则使用视频窗
				videoRemoteRendererNumber++;
				videoRendererBean.setRenderer(renderer);
				setTextVew(videoRendererBean,data);
				videoRendererlList.set(i, videoRendererBean);//重新设置已占用数据
				VideoRendererGui.update(renderer, videoRendererBean.getX(), videoRendererBean.getY(), videoRendererBean.getWidth(), videoRendererBean.getHeight(), RendererCommon.ScalingType.SCALE_ASPECT_FILL, mirror);
				Log.e("setRemoteRendererLoc  "+i, videoRendererBean.toString());
				return ;
			}
		}
	}
	Context context;
	FrameLayout fview;


	/**
	 * 设置本地渲染
	 * @param localRender 渲染器
	 * @param mirror 是否反转成像   false 不反转   true反转
	 */
	public void setLocalRendererLoc(Callbacks localRender,boolean mirror){
		if (videoRendererlList.size() == 0){
			return;
		}
		VideoRendererBean videoRendererLocBean=videoRendererlList.get(videoRendererlList.size()-1);
		videoRendererLocBean.setRenderer(localRender);
		videoRendererlList.set(videoRendererlList.size()-1, videoRendererLocBean);//重新设置已占用数据
		Lg.i("setLocalRendererLoc  "+(videoRendererlList.size()-1), videoRendererLocBean.toString());

		Lg.i("远程视频数", "Number "+videoRemoteRendererNumber);
		if(videoRemoteRendererNumber<=0){//如果没有远程视频，把本地的设置全屏
			VideoRendererGui.update(videoRendererLocBean.getRenderer(), 0, 0, 100, 100, RendererCommon.ScalingType.SCALE_ASPECT_FILL, false);
		}else{
			VideoRendererGui.update(localRender, videoRendererLocBean.getX(), videoRendererLocBean.getY(), videoRendererLocBean.getWidth(), videoRendererLocBean.getHeight(), RendererCommon.ScalingType.SCALE_ASPECT_FILL, mirror);
		}
	}
	/**
	 * 移除渲染
	 * @param
	 */
	public void setRemoveRenderer(Callbacks renderer){
		for (int i = 0; i < videoRendererlList.size(); i++) {
			VideoRendererBean videoRendererBean=videoRendererlList.get(i);
			//unpublish的处理，如果已经存在则使用
			if(videoRendererBean.getRenderer()!=null&&videoRendererBean.getRenderer()==renderer){
				if(videoRendererBean.getTextView()!=null) {
					videoRendererBean.getTextView().setVisibility(View.GONE);
				}
				videoRemoteRendererNumber--;
				videoRendererBean.setRenderer(null);
				videoRendererlList.set(i, videoRendererBean);//重新设置已占用数据
				VideoRendererGui.remove(renderer);
				return ;
			}
		}
	}
	public void setTextVew(VideoRendererBean videoRendererBean,String txt){
		int X=0,Y=0;
		int textHeight;
		TextView myTextView = new TextView(context);
		myTextView.setText(txt+videoRemoteRendererNumber);
		myTextView.setTextSize(18);
		myTextView.setTextColor(Color.GRAY);
//        myTextView.setBackgroundColor(Color.GRAY);  
		myTextView.bringToFront();
		myTextView.setShadowLayer(2, 1, 1, Color.WHITE);
		myTextView.setGravity(Gravity.CENTER_HORIZONTAL);
		textHeight=myTextView.getLineHeight()+5;
		switch (videoRemoteRendererNumber) {
			case 1:
				X=0;
				Y=screenHeight/3-textHeight ;
				break;
			case 2:
				X=screenWidth/2;
				Y=screenHeight/3-textHeight ;
				break;
			case 3:
				X=0;
				Y=screenHeight/3*2-textHeight ;
				break;
			case 4:
				X=screenWidth/2;
				Y=screenHeight/3*2-textHeight ;
				break;
			case 5:
				X=0;
				Y=screenHeight/3*3-textHeight ;
				break;
			default:
				break;
		}

		myTextView.setX(X);
		myTextView.setY(Y);
		fview.addView(myTextView,screenWidth/2,LayoutParams.WRAP_CONTENT);
		videoRendererBean.setTextView(myTextView);
	}
	boolean isFullScreen=false;
	VideoRendererBean fullScreenVideoRendererBean;
	/**
	 * 设置全屏
	 * @param touchX
	 * @param touchY
	 */
	public void setFullScreen(int touchX,int touchY){
//    	if(!Methods.isFastDoubleClick(800)){
//    		return;
//    	}
//    	Log.e("FullScreen", "touchX："+touchX+"   touchY："+touchY);
		int index = 0;
		int column = (touchX-screenWidth/columnNumber)>0?2:1;
		int row = touchY/(screenHeight/rowNumber)+1;
		if(column==1&&row>=column){
			index=(row-column)+row;
		}else{
			index=row*column;
		}
		Log.e("点击块"+index, "column "+column+"    row "+row);
		VideoRendererBean videoRendererBean=videoRendererlList.get(index-1);//取得最大化
		if(isFullScreen){
			if(fullScreenVideoRendererBean!=null){
//    			VideoRendererGui.update(fullScreenVideoRendererLocBean.getRenderer(), fullScreenVideoRendererLocBean.getX(), fullScreenVideoRendererLocBean.getY(), fullScreenVideoRendererLocBean.getWidth(), fullScreenVideoRendererLocBean.getHeight(), VideoRendererGui.ScalingType.SCALE_ASPECT_FILL, false);
				for (int i = 0; i < videoRendererlList.size(); i++) {//还原显示
					VideoRendererBean otherVideoRendererBean=videoRendererlList.get(i);
					if(otherVideoRendererBean.getRenderer()!=null){
						VideoRendererGui.update(otherVideoRendererBean.getRenderer(), otherVideoRendererBean.getX(), otherVideoRendererBean.getY(), otherVideoRendererBean.getWidth(), otherVideoRendererBean.getHeight(), RendererCommon.ScalingType.SCALE_ASPECT_FILL, false);
						if(otherVideoRendererBean.getTextView()!=null){
							otherVideoRendererBean.getTextView().setVisibility(View.VISIBLE);
						}
					}
				}
			}
			fullScreenVideoRendererBean=null;
			isFullScreen=false;
		}else{//如果未全屏则设置最大化
			if(videoRendererBean.getRenderer()!=null){//设置点击到的为最大化
				fullScreenVideoRendererBean=videoRendererBean;
				VideoRendererGui.update(videoRendererBean.getRenderer(), 0, 0, 100, 100, RendererCommon.ScalingType.SCALE_ASPECT_FILL, false);
				for (int i = 0; i < videoRendererlList.size(); i++) {//渲染分层问题处理，最小化其他渲染
					VideoRendererBean otherVideoRendererLocBean=videoRendererlList.get(i);
					if(otherVideoRendererLocBean.getTextView()!=null){
						otherVideoRendererLocBean.getTextView().setVisibility(View.GONE);
					}
					if(otherVideoRendererLocBean.getRenderer()!=null){
						if(i!=(index-1)){//除去最大化
							VideoRendererGui.update(otherVideoRendererLocBean.getRenderer(), 0, 0, 1, 1, RendererCommon.ScalingType.SCALE_ASPECT_FILL, false);
						}
					}
				}
			}
			isFullScreen=true;
		}
	}
	public Stack<VideoRendererBean> getVideoRendererList() {
		return videoRendererlList == null ? new Stack<>() : videoRendererlList;
	}
	public void setVideoRendererList(Stack<VideoRendererBean> videoRendererList) {
		this.videoRendererlList = videoRendererList;
	}

}
